from django.db import models

from utils.common import BaseModel
from apps.common.account.models import Account

"""
数据库中的file都保存为
[
    {
        "url": "survey_system/question/20220922164236-18左.png", 
        "name": "20220922164236-18左.png", 
        "content_type": "image/png"
    }, 
    {
        "url": "survey_system/question/20220922164240-18右.png", 
        "name": "20220922164240-18右.png", 
        "content_type": "image/png"
    }
]
这种格式，也就是[{}, {}, ...]这样的，没文件的话为None或者[], url为相对路径, name为文件名, content_type为文件格式
"""


# 目前有 id-name: 1-批判性思维 2-心理健康 3-审美素养
class Module(BaseModel):
    name = models.CharField(verbose_name='模块名', max_length=100)
    codename = models.CharField(verbose_name='英文名', max_length=200, unique=True)

    cover = models.JSONField(verbose_name='课程封面', default=[])
    introduction = models.TextField(verbose_name='课程概述', default='')
    target = models.CharField(verbose_name='课程目标', max_length=200, default='')

    class Meta:
        verbose_name_plural = verbose_name = '模块'
        db_table = 'survey_system_question_module'


# 模块的教师团队(实训项目不需要用到)
class ModuleTeam(BaseModel):
    module = models.ForeignKey(Module, verbose_name='所属模块', on_delete=models.CASCADE)
    account = models.ForeignKey(Account, verbose_name="关联的账号", on_delete=models.CASCADE)
    introduction = models.TextField(verbose_name='介绍', default='')
    photo = models.JSONField(verbose_name='头像', default=[])

    class Meta:
        verbose_name_plural = verbose_name = '教师团队'
        db_table = 'survey_system_module_team'


# 实训项目不需要用到
class Swiper(BaseModel):
    cover = models.JSONField(verbose_name='小程序首页轮播图')
    account = models.ForeignKey(Account, verbose_name='上传者', null=True, blank=True, on_delete=models.CASCADE)
    is_active = models.BooleanField(verbose_name='是否启用', default=True)

    class Meta:
        db_table = "survey_system_swiper"
        verbose_name = "小程序首页轮播图"
        verbose_name_plural = verbose_name


# 问题分类
class QuestionCategory(BaseModel):
    module = models.ForeignKey(Module, verbose_name='所属模块', on_delete=models.CASCADE)
    name = models.CharField(max_length=100, verbose_name='分类名')
    is_active = models.BooleanField(verbose_name='当前是否可用', default=True)

    class Meta:
        verbose_name_plural = verbose_name = '题目分类'
        db_table = 'survey_system_question_category'


# 题库里的问题
class Question(BaseModel):
    module = models.ForeignKey(Module, verbose_name='所属模块', on_delete=models.CASCADE)
    name = models.TextField(verbose_name='题目', null=False)
    file = models.JSONField(verbose_name='题目相关附件')
    desc = models.TextField(verbose_name='问题描述', default='')
    category = models.ForeignKey(QuestionCategory, verbose_name='问题分类', null=True, on_delete=models.SET_NULL)
    is_required = models.BooleanField(default=True, verbose_name='是否为必填')
    type = models.PositiveSmallIntegerField(verbose_name='题型(1-单选 2-多选 3-简答题 4-文件上传题)')

    # 针对多选题的
    mul_choice_score_type = models.PositiveSmallIntegerField(verbose_name='针对多选题的计算总分类型'
                                                                          '(1-全部选项得分相加 2-全选正确答案才能得分)',
                                                             default=1)
    is_need_limit = models.BooleanField(verbose_name='是否需要限制多选题选项可选个数', default=False)
    limit_choice_count = models.PositiveSmallIntegerField(verbose_name='针对多选题的限选个数', default=2, null=True)

    # 针对文件上传题的
    answer_need_text = models.BooleanField(verbose_name='针对文件上传题的回答，是否需要文字回答', default=False)
    answer_need_option = models.BooleanField(verbose_name='针对文件上传题的回答，是否需要选项', default=False)
    option_type = models.PositiveSmallIntegerField(verbose_name='针对需要选项的文件上传题，选项类型(1-单选 2-多选)',
                                                   default=1)

    score = models.IntegerField(verbose_name='满分分值', default=0)
    is_active = models.BooleanField(verbose_name='当前是否可用', default=True)

    class Meta:
        verbose_name_plural = verbose_name = '题库里的题目'
        db_table = 'survey_system_question'


# 问题选项，简答题没有选项
class QuestionOption(BaseModel):
    question = models.ForeignKey(Question, verbose_name='所属问题', on_delete=models.CASCADE, related_name='options')
    num = models.CharField(verbose_name='选项编号', default='', max_length=10)
    name = models.TextField(verbose_name='选项', default='')
    is_answer = models.BooleanField(verbose_name='该项是否为正确答案', default=False)
    file = models.JSONField(verbose_name='该选项相关附件', default=None)
    score = models.IntegerField(verbose_name='分值', default=0)

    class Meta:
        verbose_name_plural = verbose_name = '题目选项'
        db_table = 'survey_system_question_option'


# 问卷
class Questionnaire(BaseModel):
    account = models.ForeignKey(Account, verbose_name='创建人', null=True, on_delete=models.SET_NULL)
    module = models.ForeignKey(Module, verbose_name='所属模块', on_delete=models.CASCADE)
    name = models.CharField(verbose_name='问卷名', max_length=100)
    is_active = models.BooleanField(verbose_name='当前是否可用', default=True)

    class Meta:
        verbose_name_plural = verbose_name = '问卷'
        db_table = 'survey_system_questionnaire'


# 问卷题目的分类(创建问卷的时候需要创建分类，再选取该分类下的题目)
class QuestionnaireQsCate(models.Model):
    module = models.ForeignKey(Module, verbose_name='所属模块', on_delete=models.CASCADE)
    questionnaire = models.ForeignKey(Questionnaire, verbose_name='所属问卷', on_delete=models.CASCADE,
                                      related_name='category')
    name = models.CharField(max_length=100, verbose_name='分类名')
    sort_num = models.PositiveSmallIntegerField(verbose_name='分类排序', default=100)

    class Meta:
        verbose_name_plural = verbose_name = '问卷题目的分类'
        db_table = 'survey_system_questionnaire_qs_cate'


# 问卷题目
class QuestionnaireQuestion(BaseModel):
    category = models.ForeignKey(QuestionnaireQsCate, verbose_name='所属问卷题目的分类', on_delete=models.CASCADE)
    question = models.ForeignKey(Question, verbose_name='题目', on_delete=models.CASCADE)
    sort_num = models.IntegerField(verbose_name='题目排序编号', default=0)

    class Meta:
        verbose_name_plural = verbose_name = '问卷题目'
        db_table = 'survey_system_questionnaire_qs'
